﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace ASoft.Led
{
    public class LedPic
    {
        public LedPic(LedCfg ledCfg, string ledPath)
        {
            this.LedCfg = ledCfg;
            this.LedPath = ledPath;
        }

        public string LedPath { get; private set; }

        public LedCfg LedCfg { get; private set; }

        public string TemplatePhecPath { get { return Path.Combine(LedPath, "phec.bmp"); } }

        public string PhecPath { get { return Path.Combine(LedPath, "phec1.bmp"); } }

        public string TemplateRcellPath { get { return Path.Combine(LedPath, "rcell.bmp"); } }

        public string RcellPath { get { return Path.Combine(LedPath, "rcell1.bmp"); } }

        void CreatePic(string templatePath, string createPath, List<DrawItem> items)
        {
            Bitmap bmp = new Bitmap(templatePath);
            Graphics graphics = Graphics.FromImage(bmp);
            Font font = new Font("宋体", 11);
            Brush brush = new SolidBrush(Color.Red);
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            items.ForEach(p =>
                {
                    graphics.DrawString(p.Text, font, brush, p.X, p.Y, stringFormat);
                });
            bmp.Save(createPath, System.Drawing.Imaging.ImageFormat.Bmp);
            graphics.Dispose();
            bmp.Dispose();
        }

        public void CreatePhec(List<PhecData> phecDatas)
        {
            List<DrawItem> drawItems = new List<DrawItem>();
            var datas = phecDatas.Take(7).ToList();
            for (int i = 1; i <= datas.Count; i++)
            {
                var phecItem = typeof(LedCfg).GetProperty("PhecItem" + i.ToString()).GetValue(this.LedCfg, null) as PhecItem;
                var data = datas[i - 1];
                drawItems.Add(new DrawItem(data.Name, phecItem.NameX, phecItem.NameY));
                drawItems.Add(new DrawItem(data.Ph, phecItem.PhX, phecItem.PhY));
                drawItems.Add(new DrawItem(data.Ec, phecItem.EcX, phecItem.EcY));
            }
            CreatePic(TemplatePhecPath, PhecPath, drawItems);
        }

        public void CreateRCell(WeatherData wData, RData rData1, RData rData2)
        {
            List<DrawItem> drawItems = new List<DrawItem>();

            ProcessR(typeof(RData), rData1, typeof(RItem), LedCfg.R1, 0, 7, drawItems);
            ProcessR(typeof(RData), rData2, typeof(RItem), LedCfg.R2, 0, 7, drawItems);
            ProcessR(typeof(WeatherData), wData, typeof(WeatherItem), LedCfg.Weather, 1, 5, drawItems);

            CreatePic(TemplateRcellPath, RcellPath, drawItems);
        }

        void ProcessR(Type dataType, object rData, Type itemType, object rItem, int startIndex, int endIndex, List<DrawItem> drawItems)
        {
            var tRItem = typeof(RItem);
            for (int i = startIndex; i <= endIndex; i++)
            {
                int x = (int)itemType.GetProperty(String.Format("Cell{0}X", i)).GetValue(rItem, null);
                int y = (int)itemType.GetProperty(String.Format("Cell{0}Y", i)).GetValue(rItem, null);
                string text = (string)dataType.GetProperty(String.Format("Text{0}", i)).GetValue(rData, null);
                drawItems.Add(new DrawItem(text, x, y));
            }
        }

    }

    public class DrawItem
    {
        public DrawItem(string text, int x, int y)
        {
            this.Text = text;
            this.X = x;
            this.Y = y;
        }

        public string Text { get; private set; }
        public int X { get; private set; }
        public int Y { get; private set; }
    }

    
}
